﻿Public Class xtf_ABMBase
    Public frm_listado As Form = Nothing
    Public flg_Modificado As Boolean = False
    Public Event eve_ControlModificado(ByVal obj_Control As Control)
    Private col_ControlesValidar As List(Of Control)
    Public bol_comprobarCerrado As Boolean = True

    Private Sub xtf_ABMBase_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If Me.Visible Then
            Dim miflag As Boolean = flg_Modificado
            CargarObjetos()
            flg_Modificado = miflag
            btn_Guardar.Enabled = miflag
            Dim focused As Boolean = False
            For Each Group As Object In lac_general.Items
                Select Case Group.GetType.ToString
                    Case GetType(DevExpress.XtraLayout.LayoutControlGroup).ToString
                        For Each Item As Object In CType(Group, DevExpress.XtraLayout.LayoutControlGroup).Items
                            Select Case Item.GetType.ToString
                                Case GetType(DevExpress.XtraLayout.LayoutControlItem).ToString
                                    If UCase((CType(Item, DevExpress.XtraLayout.LayoutControlItem).Control).Name) = UCase("txt_nombre") Or UCase((CType(Item, DevExpress.XtraLayout.LayoutControlItem).Control).Name) = UCase("txt_nombres") Or (UCase((CType(Item, DevExpress.XtraLayout.LayoutControlItem).Control).Name).Contains(UCase("txt_nombre")) And Not UCase((CType(Item, DevExpress.XtraLayout.LayoutControlItem).Control).Name).Contains(UCase("corto"))) Then
                                        If Not focused Then
                                            CType(Item, DevExpress.XtraLayout.LayoutControlItem).Control.Focus()
                                            focused = True
                                        End If
                                    End If
                            End Select
                        Next
                End Select
            Next
        End If
    End Sub

    Private Sub xtf_ABMBase_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

    End Sub

    Private Sub xtf_ABMBase_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If bol_comprobarCerrado Then
            e.Cancel = True
            btn_Cerrar.PerformClick()
        End If
    End Sub

    Public Sub New()
        InitializeComponent()
        Me.Icon = ico_AppIcono
    End Sub

    Public Overridable Sub CargarObjetos()
    End Sub

    Public Overloads Sub Nuevo(ByRef listado As Form)
    End Sub

    Public Overloads Sub Modificar(ByRef objeto As Object, ByRef listado As Form, ByRef row As DataRow)
    End Sub

    Public Sub AgregarValidaciones()
        AgregarValidacionesGlobal(Me, Me.lac_general, col_ControlesValidar)
        PerzonalizarControles(lac_general.Items)
    End Sub

#Region "Controlar Modificaciones"
    Public Overridable Sub ControlarModificaciones(ByRef coleccion As Object)
        For Each Group As Object In coleccion
            Select Case Group.GetType.ToString
                Case GetType(DevExpress.XtraLayout.LayoutControlGroup).ToString
                    For Each Item As Object In CType(Group, DevExpress.XtraLayout.LayoutControlGroup).Items
                        Select Case Item.GetType.ToString
                            Case GetType(DevExpress.XtraLayout.LayoutControlItem).ToString
                                ControlarModificacion(CType(Item, DevExpress.XtraLayout.LayoutControlItem).Control)
                        End Select
                    Next
            End Select
        Next
    End Sub

    Public Sub ControlarModificacion(ByVal obj As Object)
        Select Case obj.GetType.Name
            Case GetType(DevExpress.XtraEditors.TextEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.TextEdit).EditValueChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.LookUpEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.LookUpEdit).EditValueChanging, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.ComboBoxEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.ComboBoxEdit).EditValueChanging, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.MemoEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.MemoEdit).EditValueChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraGrid.GridControl).Name
                Try
                    AddHandler CType(CType(obj, DevExpress.XtraGrid.GridControl).MainView, DevExpress.XtraGrid.Views.Grid.GridView).RowCountChanged, AddressOf obj_ValueChanged
                Catch ex As Exception
                End Try
                Try
                    AddHandler CType(CType(obj, DevExpress.XtraGrid.GridControl).MainView, DevExpress.XtraGrid.Views.Grid.GridView).CellValueChanging, AddressOf obj_ValueChanged
                Catch ex As Exception
                End Try
            Case GetType(DevExpress.XtraEditors.Repository.RepositoryItemTextEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.Repository.RepositoryItemTextEdit).EditValueChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.ImageListBoxControl).Name
                AddHandler CType(obj, DevExpress.XtraEditors.ImageListBoxControl).Validated, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.CheckEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.CheckEdit).CheckedChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.SimpleButton).Name
                AddHandler CType(obj, DevExpress.XtraEditors.SimpleButton).Click, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.PanelControl).Name
                For Each Control As Object In CType(obj, DevExpress.XtraEditors.PanelControl).Controls
                    ControlarModificacion(Control)
                Next
            Case GetType(DevExpress.XtraEditors.RadioGroup).Name
                AddHandler CType(obj, DevExpress.XtraEditors.RadioGroup).SelectedIndexChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.PictureEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.PictureEdit).EditValueChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraTreeList.TreeList).Name
                AddHandler CType(obj, DevExpress.XtraTreeList.TreeList).BeforeCheckNode, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.DateEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.DateEdit).DateTimeChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.ButtonEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.ButtonEdit).TextChanged, AddressOf obj_ValueChanged
            Case GetType(DevExpress.XtraEditors.TimeEdit).Name
                AddHandler CType(obj, DevExpress.XtraEditors.TimeEdit).EditValueChanging, AddressOf obj_ValueChanged

        End Select
    End Sub

    Public Sub obj_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            If sender.enabled Then
                RaiseEvent eve_ControlModificado(sender)
                flg_Modificado = True : btn_Guardar.Enabled = True : bbi_guardar.Enabled = True
            End If
        Catch ex As Exception
            RaiseEvent eve_ControlModificado(sender)
            flg_Modificado = True : btn_Guardar.Enabled = True : bbi_guardar.Enabled = True
        End Try
    End Sub
#End Region

    Public Overridable Function ComprobarDatos() As Boolean
    End Function

    Public Function ControlarValidaciones() As Boolean
        Return ControlarValidacionesGlobal(col_ControlesValidar)
    End Function

    Private Sub bbi_Guardar_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_guardar.ItemClick
        btn_Guardar.PerformClick()
    End Sub

    Public Overridable Sub bbi_imprimir_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_imprimir.ItemClick
        ShowGridPreview(Me.Text, lac_general, True)
    End Sub

    Private Sub bbi_cerrar_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_cerrar.ItemClick
        Me.Close()
    End Sub

    Private Sub xtf_ABMBase_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsNothing(UsuarioLOG) Then
            If Not UsuarioLOG.esroot Then
                bbi_guardarXML.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
                bbi_restaurarXML.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
                lac_general.AllowCustomizationMenu = False
            End If
        End If
        If System.IO.File.Exists(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & sender.name & ".xml") Then
            lac_general.RestoreLayoutFromXml(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & sender.name & ".xml")
            If System.IO.File.Exists(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & sender.name & ".size") Then
                Dim ini As New cls_INI
                Me.Width = ini.IniGet(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & sender.name & ".size", "General", "Width", Me.Width)
                Me.Height = ini.IniGet(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & sender.name & ".size", "General", "Height", Me.Height)
            End If
        End If
    End Sub

    Private Sub xtf_ABMBase_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        'Comprobar si la ventana ya esta abierta
        For Each frm As Object In Application.OpenForms()

            If Me.Text = frm.text And Me.Handle <> frm.handle Then
                frm.focus()
                Me.Close()
                Exit Sub
            End If
        Next
            '----------------------------------------
    End Sub

    Private Sub btn_guardarSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_guardarSalir.Click
        If ComprobarDatos() Then
            btn_Guardar.PerformClick()
            btn_Cerrar.PerformClick()
        End If
    End Sub


    Private Sub btn_Guardar_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Guardar.EnabledChanged
        btn_guardarSalir.Enabled = btn_Guardar.Enabled
    End Sub

    Private Sub bbi_guardarXML_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_guardarXML.ItemClick
        If Not System.IO.Directory.Exists(Application.StartupPath & "\" & str_subNombreFile & "plantillas") Then
            System.IO.Directory.CreateDirectory(Application.StartupPath & "\" & str_subNombreFile & "plantillas")
        End If
        lac_general.SaveLayoutToXml(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".xml")
        If System.IO.File.Exists(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".size") Then
            System.IO.File.Delete(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".size")
        End If
        Dim stream As New System.IO.StreamWriter(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".size", IO.FileMode.Create)
        stream.WriteLine("[General]" & vbNewLine & "Width=" & Me.Width & vbNewLine & "Height=" & Me.Height)
        stream.Flush()
        stream.Close()
        ErrManagerShow("Configuracion root", "La personalizacion del formulario se guardo correctamente en:#" & Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".xml#" & Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".size", DevEx.xtf_ErrManagerAlerta.errManagerImg.img_Informacion, DevEx.xtf_ErrManagerAlerta.errManagerBtn.btn_Aceptar)
    End Sub

    Private Sub bbi_restaurarXML_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_restaurarXML.ItemClick
        If Not System.IO.Directory.Exists(Application.StartupPath & "\" & str_subNombreFile & "plantillas") Then
            System.IO.Directory.CreateDirectory(Application.StartupPath & "\" & str_subNombreFile & "plantillas")
        End If
        If System.IO.File.Exists(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".xml") Then
            System.IO.File.Delete(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".xml")
        End If
        If System.IO.File.Exists(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".size") Then
            System.IO.File.Delete(Application.StartupPath & "\" & str_subNombreFile & "plantillas\" & str_subNombreFile & Me.Name & ".size")
        End If
        ErrManagerShow("Configuracion root", "La personalizacion del formulario se restauro,# reinicie el formulario para concluir el proceso.", DevEx.xtf_ErrManagerAlerta.errManagerImg.img_Informacion, DevEx.xtf_ErrManagerAlerta.errManagerBtn.btn_Aceptar)
    End Sub

End Class